package uilayer;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Textbased UI which allows the user to input information about Main Menu.
 * @author (Søren Kaae)
 * @version 09.12.2014
 */
public class MainMenuUI
{
    private VareUI vUI;
    private OrdreUI oUI;
    private PersonUI pUI;
    private String loggetInd;
    private static MainMenuUI instance;

    /**
     * The main method.
     *
     * @param args the arguments
     */
    public static void main(String[] args)
    {
        MainMenuUI mainMenuUI;
        mainMenuUI = new MainMenuUI();
        mainMenuUI.loginMenu();
    }

    /**
     * Instantiates a new main menu ui.
     */
    private MainMenuUI()
    {
        vUI = VareUI.getInstance();
        oUI = OrdreUI.getInstance();
        pUI = PersonUI.getInstance();
    }
    
    public static MainMenuUI getInstance()
    {
        if(instance == null)
        {
            instance = new MainMenuUI();
        }
        return instance;
    }
    
    
    /**
     * Main menu.
     * 
     * Checks the userinput in the writeMainMenu method.
     * Each choice results in different methods.
     */
    public void mainMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writeMainMenu(); 
            switch(choice)
            {
                case 1: 
                pUI.personMenu();
                break;
                case 2:
                vUI.vareMenu(loggetInd);
                break;
                case 3:
                oUI.ordreMenu();
                break;
                case 4:
                System.out.println("Medarbejdernummeret på den person, som er logget ind:");
                System.out.println(loggetInd);
                mainMenu();
                break;
                case 5:
                //save data!
                saveAll();
                //logud
                loginMenu();
                break;
                default:
                //save data!
                saveAll();
                //exit
                exit = true; 
            }
        }
    }

    /**
     * Method loginMenu
     *
     * This menu is the first thing our user is presented with. It asks for a username and a password.
     * If the given parameters doesn't compare to any employee in the system, the user will get an error message.
     */
    public void loginMenu()
    {
        pUI.loadAnsatte();
        pUI.loadKunder();
        vUI.loadVare();
        oUI.loadOrdre();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Login ***");
        System.out.println("Angiv brugernavn: ");
        String brugernavn = keyboard.nextLine();
        System.out.println("Angiv password: ");
        String password = keyboard.nextLine();
        if(pUI.kontrolLogin(brugernavn, password))
        {
            String s = pUI.loggetInd(brugernavn);
            setLoggetInd(s);
            mainMenu();
        }
        else
        {                
            System.out.println("Forkert password!!");
            // clear screen
            System.out.println('\u000C');
            loginMenu();
        }
    }

    /**
     * Method setLoggetInd
     *
     * Changes the person that is logged into the system.
     * 
     * @param s The employee ID
     */
    public void setLoggetInd(String s)
    {
        this.loggetInd = s;
    }

    public String getLoggetInd()
    {
        return loggetInd;
    }

    /**
     * Write main menu.
     *
     * @return the int
     */
    public int writeMainMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Main Menu ***");
        System.out.println(" (1) Personer");
        System.out.println(" (2) Varer");
        System.out.println(" (3) Ordre");
        System.out.println(" (4) Logget ind som");
        System.out.println(" (5) Log ud");
        System.out.println(" (6) Exit");
        System.out.print("\n Choose: ");
        int choise;
        while(!keyboard.hasNextInt())
        {
            System.out.println("Indtast venligst et tal!");
            keyboard.next(); 
        }
        choise = keyboard.nextInt();
        System.out.print("\n");
        return choise;
    }
    
    public void saveAll()
    {
        pUI.save();
        oUI.save();
        vUI.save();
    }

}
